package com.zhang.bingmei_incomeexpenditure.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhang.bingmei_incomeexpenditure.entity.commom.DataScope;
import com.zhang.bingmei_incomeexpenditure.entity.user.User;
import com.zhang.bingmei_incomeexpenditure.service.CommCityService;
import com.zhang.bingmei_incomeexpenditure.entity.comCity.CommCity;
import com.zhang.bingmei_incomeexpenditure.mapper.TGoodsMapper;
import com.zhang.bingmei_incomeexpenditure.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zsj
 * @description
 * @createDate 2025/10/17 15:07
 * @Entity com.zhang.bingmei_incomeexpenditure.controller.entity.CommCityController
 * */


@RestController
@RequestMapping("/comm-cities")
@Slf4j
public class CommCityController {

    @Resource
    private CommCityService commCityService;
    @Resource
    private TGoodsMapper tGoodsMapper;
    @Resource
    private UserService userService;
    @Qualifier("commCityMapper")
    @Autowired
    private BaseMapper<CommCity> baseMapper;

    @GetMapping("/dataScope")
    public List<CommCity> dataScope(@RequestParam String userId, @RequestParam Integer level) {

        //通过getDataScope()获取当前用户所在城市，return所在城市 DataScope
        //通过listByDataScopeArea()获取所在城市下的市县区街道，return
        //将
        DataScope dataScope = userService.getDataScope(userId, level);
        User user = userService.getById(userId);

        log.info(StrUtil.toString(dataScope));
        //拿到当前用户的区域权限
        CommCity commCity = new CommCity();
        dataScope.getArea();
        List<String> strings = commCityService.listByDataScopeArea(dataScope, user);
        //将编码转换成name
        QueryWrapper<CommCity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("code",strings).eq("deleted",0);
        List<CommCity> commCities = baseMapper.selectList(queryWrapper);
        return commCities;
    }

    // http://localhost:8181/comm-cities/all
    @GetMapping("/all")
    public List<String> findAll() {
/*        commCityService.list().stream()
                .forEach(System.out::println);
        List<CommCity> list = commCityService.list();

        List<String> mergerName = list.stream()
                .map(CommCity::getMergerName)
                .collect(Collectors.toList());*/

        List<CommCity> list = commCityService.list();
        List<String> cityDistricts = list.stream()
                .filter(city -> city.getLevelType().equals("2")) //过滤出level_type==2的city
                .map(city -> city.getName())   //拿到其name
                .distinct() //去重相同的name
                .collect(Collectors.toList()); //收集起来


        return cityDistricts;
    }

    // http://localhost:8181/comm-cities/aaa?userRoleLevel=1
    @GetMapping("/aaa")
    public List<String> findDistricts(@RequestParam(defaultValue = "-1") Integer userRoleLevel) {
        List<CommCity> list = commCityService.list();
        List<String> cityDistricts = Collections.emptyList();
        String area = "";

        if (userRoleLevel == -1 ){
            log.info("用户无角色，已返回空列表...");
            return Collections.emptyList();
            //throw new RuntimeException("用户色\");无角
            // 1：系统管理员  2：省级管理员
        }else if (userRoleLevel == 1 || userRoleLevel == 2){
            // 看到的列表是全省的市辖区，依次显示海口市秀英区、海口龙华区等海南所有的市县区(不含街道，乡镇)

            //TODO: 方法提取：调用comm_city表中的方法，获取level_code字段，获取其对应的name字段。
            cityDistricts = list.stream()
                    .filter(city -> city.getLevelType().equals("3"))
                    .map(CommCity::getCode)
                    .distinct()
                    .collect(Collectors.toList());

        }else if (userRoleLevel == 3){  // 3：市级管理员
          // ①海口市460100、三亚市460200、三沙市460300 看到各区的数据
            cityDistricts = list.stream()
                    .filter(city ->
                            city.getLevelCode().contains("460100") ||
                                    city.getLevelCode().contains("460200") ||
                                    city.getLevelCode().contains("460300"))
                    .filter(city -> city.getLevelType() == "4")
                    .map(CommCity::getCode)
                    .distinct()
                    .collect(Collectors.toList());
            // ②其他市县仅看到本市县的数据(含乡镇、街道)


            // ①海口市460100、三亚市460200、三沙市460300 看到各区的数据
            cityDistricts = list.stream()
                    .filter(city -> {
                        String levelCode = city.getLevelCode();

                        // ①海口市460100、三亚市460200、三沙市460300 看到各区的数据
                        boolean isSpecialCity = levelCode.contains("460100") ||
                                levelCode.contains("460200") ||
                                levelCode.contains("460300");
                        if (isSpecialCity) {
                            // 特殊城市：只看区级数据(levelType="4")
                            return city.getLevelType().equals("3");
                        } else {
                            // ②其他市县仅看到本市县的数据(含乡镇、街道)
                            return city.getLevelType().equals("3");
                        }
                    })
                    .map(CommCity::getCode)
                    .distinct()
                    .collect(Collectors.toList());

        }else if (userRoleLevel == 4){  //4：区级管理员
            // 仅看到本区的数据(含街道、乡镇)
            cityDistricts = list.stream()
                    .filter(city -> city.getLevelType().equals("4"))
                    .map(CommCity::getCode)
                    .distinct()
                    .collect(Collectors.toList());
        }else if (userRoleLevel == 999){

        }


        //2.3 根据地区集合去数据库查询数据

        //3.返回数据封装

        return cityDistricts;
    }
/*
    // http://localhost:8181/comm-cities/bbb?userRoleLevel=1
    @GetMapping("/bbb")
    public List<Map<String,String>> findDistrictsAndCode(@RequestParam(defaultValue = "-1") Integer userRoleLevel) {}String> findDistrictsAndCode(@RequestParam(defaultValue = "-1") Integer userRoleLevel) {
        List<CommCity> list = commCityService.list();
        List<Map<String,String>> cityDistricts = null;
        String area = "";

        if (userRoleLevel == -1 ){
            log.info("用户无角色，已返回空列表...");
            return Collections.emptyList();
            //throw new RuntimeException("用户色\");无角
            // 1：系统管理员  2：省级管理员
        }else if (userRoleLevel == 1 || userRoleLevel == 2){
            // 看到的列表是全省的市辖区，依次显示海口市秀英区、海口龙华区等海南所有的市县区(不含街道，乡镇)

            //TODO: 方法提取：调用comm_city表中的方法，获取level_code字段，获取其对应的name字段。
            cityDistricts = list.stream()
                    .filter(city -> city.getLevelType().equals("3"))
                    .map(CommCity::getCode)
                    .distinct()
                    .collect(Collectors.toList());

        }else if (userRoleLevel == 3){  // 3：市级管理员
          // ①海口市460100、三亚市460200、三沙市460300 看到各区的数据
            cityDistricts = list.stream()
                    .filter(city ->
                            city.getLevelCode().contains("460100") ||
                                    city.getLevelCode().contains("460200") ||
                                    city.getLevelCode().contains("460300"))
                    .filter(city -> city.getLevelType() == "4")
                    .map(CommCity::getCode)
                    .distinct()
                    .collect(Collectors.toList());
            // ②其他市县仅看到本市县的数据(含乡镇、街道)


            // ①海口市460100、三亚市460200、三沙市460300 看到各区的数据
            cityDistricts = list.stream()
                    .filter(city -> {
                        String levelCode = city.getLevelCode();

                        // ①海口市460100、三亚市460200、三沙市460300 看到各区的数据
                        boolean isSpecialCity = levelCode.contains("460100") ||
                                levelCode.contains("460200") ||
                                levelCode.contains("460300");
                        if (isSpecialCity) {
                            // 特殊城市：只看区级数据(levelType="4")
                            return city.getLevelType().equals("3");
                        } else {
                            // ②其他市县仅看到本市县的数据(含乡镇、街道)
                            return city.getLevelType().equals("3");
                        }
                    })
                    .map(CommCity::getCode)
                    .distinct()
                    .collect(Collectors.toList());

        }else if (userRoleLevel == 4){  //4：区级管理员
            // 仅看到本区的数据(含街道、乡镇)
            cityDistricts = list.stream()
                    .filter(city -> city.getLevelType().equals("4"))
                    .map(CommCity::getCode)
                    .distinct()
                    .collect(Collectors.toList());
        }else if (userRoleLevel == 999){

        }


        //2.3 根据地区集合去数据库查询数据

        //3.返回数据封装

        return cityDistricts;
    }*/

}
